package com.xiaoyi.intentsdklibrary.Bean;

import android.content.Context;
import com.xiaoyi.intentsdklibrary.Bean.DaoMaster;
import com.xiaoyi.intentsdklibrary.Bean.MsgBeanDao;
import java.util.ArrayList;
import java.util.List;
import org.greenrobot.greendao.query.WhereCondition;

public class MsgBeanSqlUtil {
    private static final MsgBeanSqlUtil ourInstance = new MsgBeanSqlUtil();
    private MsgBeanDao mMsgBeanDao;

    public static MsgBeanSqlUtil getInstance() {
        return ourInstance;
    }

    private MsgBeanSqlUtil() {
    }

    public void initDbHelp(Context context) {
        this.mMsgBeanDao = new DaoMaster(new DaoMaster.DevOpenHelper(context, "intentsdk-db", null).getWritableDatabase()).newSession().getMsgBeanDao();
    }

    public void add(MsgBean msgBean) {
        this.mMsgBeanDao.insertOrReplace(msgBean);
    }

    public void addList(List<MsgBean> list) {
        this.mMsgBeanDao.insertOrReplaceInTx(list);
    }

    public void update(MsgBean msgBean) {
        this.mMsgBeanDao.update(msgBean);
    }

    public void delByID(String str) {
        if (str == null) {
            str = "";
        }
        ArrayList arrayList = (ArrayList) this.mMsgBeanDao.queryBuilder().where(MsgBeanDao.Properties.Script_id.eq(str), new WhereCondition[0]).list();
        if (arrayList.size() > 0) {
            this.mMsgBeanDao.deleteInTx(arrayList);
        }
    }

    public void delAll() {
        try {
            this.mMsgBeanDao.deleteInTx(this.mMsgBeanDao.queryBuilder().build().list());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public MsgBean searchByID(String str) {
        new ArrayList();
        if (str == null) {
            str = "";
        }
        try {
            ArrayList arrayList = (ArrayList) this.mMsgBeanDao.queryBuilder().where(MsgBeanDao.Properties.Grade_id.eq(str), new WhereCondition[0]).list();
            if (arrayList == null || arrayList.size() <= 0) {
                return null;
            }
            return (MsgBean) arrayList.get(0);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public List<MsgBean> searchByScriptID(String str) {
        ArrayList arrayList = (ArrayList) this.mMsgBeanDao.queryBuilder().where(MsgBeanDao.Properties.Script_id.eq(str), new WhereCondition[0]).list();
        return arrayList == null ? new ArrayList() : arrayList;
    }

    public List<MsgBean> searchAll() {
        List<MsgBean> list = this.mMsgBeanDao.queryBuilder().build().list();
        return list == null ? new ArrayList() : list;
    }
}
